﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="AliSub.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：AliSub.cs
// 创建时间：2014-12-25 18:04
// 修改时间：2014-12-25 16:07
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

namespace Payment.Albaba.PcCode
{
    #region

    using System.Collections.Generic;
    using System.Linq;

    using Payment.Albaba.Com;

    #endregion

    /// <summary>
    ///     支付宝请求类
    /// </summary>
    public class AliSub : PaySubBase
    {
        #region Fields

        /// <summary>
        ///     The ali pay basic parameter.
        /// </summary>
        private readonly AliConfig config;


        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AliSub"/> class.
        /// </summary>
        /// <param name="config">
        /// </param>
        /// <param name="payService">
        /// The pay Service.
        /// </param>
        public AliSub(AliConfig config, AliPayService payService)
        {
            this.Gateway = AliGateway.Gateway;
            this.config = config;
            this.ServiceParameter = payService;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     其他业务参数集合
        /// </summary>
        public Dictionary<string, string> ServiceParameter { get; protected set; }


        #endregion

        #region Public Methods and Operators

        protected override string Method()
        {
            return "get";
        }

        ///// <summary>
        ///// 发送提交参数
        ///// </summary>
        ///// <returns>
        ///// The <see cref="string"/>.
        ///// </returns>
        //public string Fasong()
        //{
        //    return AliCore.BuildRequest(this.Sign(), AliGateway.Gateway, this.config.InputCharset);
        //}

        ///// <summary>
        ///// 发送提交参数
        ///// </summary>
        ///// <param name="dictionary">
        ///// 签名后的参数集合
        ///// </param>
        ///// <returns>
        ///// The <see cref="string"/>.
        ///// </returns>
        //public string Fasong(Dictionary<string, string> dictionary)
        //{
        //    return AliCore.BuildRequest(dictionary, AliGateway.Gateway, this.config.InputCharset);
        //}

        /// <summary>
        /// 写入其他支付
        /// </summary>
        /// <param name="other">
        /// 其他参数集合
        /// </param>
        public void SetOtherService(Dictionary<string, string> other)
        {
            this.ServiceParameter = this.ServiceParameter.Union(other).ToDictionary(a => a.Key, b => b.Value);
        }

        protected override Dictionary<string, string> GetSubData()
        {
            var sp = this.config.Union(this.ServiceParameter).ToDictionary(a => a.Key, b => b.Value);
            this.SubData = AliCore.FilterPara(sp);
            string buildRequestMysign = this.BuildRequestMysign(this.SubData);

            this.SubData["sign"] = buildRequestMysign;
            this.SubData["sign_type"] = this.config.SignType;

            this.SubData = this.SubData;
            return this.SubData;
        }

        #endregion

        #region Methods

        /// <summary>
        /// 生成请求时的签名
        /// </summary>
        /// <param name="sPara">
        /// 请求给支付宝的参数数组
        /// </param>
        /// <returns>
        /// 签名结果
        /// </returns>
        private string BuildRequestMysign(Dictionary<string, string> sPara)
        {
            // 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
            string prestr = AliCore.CreateLinkString(sPara);

            // 把最终的字符串签名，获得签名结果
            string mysign = AliCore.Md5Sign(prestr, this.config.Key);

            return mysign;
        }

        #endregion
    }
}